package cn.hairui.blog.mapper;

import cn.hairui.blog.model.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.BaseMapper;

import java.util.List;
import java.util.Map;

/**
 * @author lihairui
 * @version V1.0
 * @date: 2019/7/28 20:14
 */
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {

    @Select("SELECT * FROM CATEGORY WHERE IS_SHOW=#{isShow} ORDER BY SHOW_ORDER ")
    List<Category> findByIsShow(String isShow);

    @Select("SELECT * FROM CATEGORY WHERE ID=#{categoryId}")
    Category find(int categoryId);

    @Select("SELECT A.ID AS categoryId,A.CATEGORY_NAME as categoryName,COUNT(B.ID) as categoryNum FROM CATEGORY A JOIN ARTICAL B ON A.ID = B.CATEGORYID GROUP BY A.ID ORDER BY A.ID ASC")
    List<Map> findCountList();

    @Select("SELECT * FROM CATEGORY ORDER BY ID DESC")
    List<Category> list();

    @Select("SELECT t1.ID AS id,t1.CATEGORY_NAME AS categoryName,CASE WHEN t1.IS_SHOW='Y' THEN 'checked' ELSE '' END AS isShow,t1.SHOW_ORDER AS showOrder,(SELECT COUNT(1) FROM ARTICAL T2 WHERE T1.ID=T2.CATEGORYID) AS articalNum FROM CATEGORY  t1 ORDER BY t1.ID ASC")
    List<Map> listMgr();

    @Update("UPDATE CATEGORY SET IS_SHOW=#{flag},SHOW_ORDER=null WHERE ID=#{id}")
    void updateIsShow(int id, String flag);

    @Select("SELECT MAX(SHOW_ORDER) FROM CATEGORY")
    int findMaxOrder();

    @Update("UPDATE CATEGORY SET SHOW_ORDER=#{orderNo} WHERE ID=#{id}")
    void updateShowOrder(int id, int orderNo);
}
